package logic

import (
	"context"
	"errors"
	"shop/cache"
	"shop/model/mysql"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateHouseLogic {
	return &UpdateHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateHouseLogic) UpdateHouse(in *shop.UpdateHouseRequest) (*shop.UpdateHouseResponse, error) {
	// todo: add your logic here and delete this line
	var err error

	result, _ := cache.Rdb.Get(l.ctx, "shopAdd").Result()
	if result == "" {
		logx.Error("已超过五分钟的时效不能进行修改了")
		return nil, errors.New("已超过五分钟的时效不能进行修改了")
	}
	var h mysql.House
	h.UserId = int(in.UserId)
	h.ID = uint(in.ShopId)
	h.Title = in.Title
	h.Info = in.Info
	h.Price = float64(in.Price)
	h.Num = int(in.Num)
	h.Image = in.Image
	mysql.DB.Find(&h)
	if err = h.Update(); err != nil {
		logx.Error("修改失败")
		return nil, errors.New("修改失败")
	}
	return &shop.UpdateHouseResponse{
		Id: int64(h.ID),
	}, nil
}
